package cn.vcjf.washcar.dict.controller;


import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.dict.entity.Dict;
import cn.vcjf.washcar.dict.service.DictCache;
import cn.vcjf.washcar.dict.service.IDictService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-09
 */
@Controller
@RequestMapping("/dict/dict")
public class DictController {
	@Resource
	private IDictService dictService;

	@RequestMapping("/addDict")
	@ResponseBody
	public TableResult addDict(Dict dict) {

		dict.preInsert();
		boolean b = dictService.insert(dict);
		//  清空缓存
		DictCache.clear();
		return TableResult.Result(b);
	}

	@RequestMapping("/updateDict")
	@ResponseBody
	public TableResult updateDict(Dict dict) {
		dict.preUpdate();
		boolean b = dictService.updateById(dict);
		//  清空缓存
		DictCache.clear();
		return TableResult.Result(b);
	}

	@RequestMapping("/findDictOne")
	@ResponseBody
	public TableResult findDictOne(Dict dict) {

		EntityWrapper<Dict> wrapper = new EntityWrapper<Dict>();
		//Todo
		if (dict.getId() != null) {
			wrapper.eq("id", dict.getId());
		}
		if (dict.getType() != null) {
			wrapper.eq("type", dict.getType());
		}
		if (dict.getLabel() != null) {
			wrapper.eq("label", dict.getLabel());
		}

		Dict result = (Dict) DictCache.get(dict.toString());
		if (result == null) {
			result = dictService.selectOne(wrapper);
			DictCache.put(dict.toString(), result);
		}
		return TableResult.Result(result);
	}

	@RequestMapping("/findDictList")
	@ResponseBody
	public TableResult findDictList(Dict dict) {

		EntityWrapper<Dict> wrapper = dict.getQueryWrapper();

		wrapper.orderBy("type");
		Page<Dict> page = new Page<Dict>(dict.getPage(), dict.getLimit());
		Page<Dict> result = (Page<Dict>) DictCache.get(dict.toString()+page);
		if (result == null) {
			wrapper.orderBy("createTime",false);
			result = dictService.selectPage(page, wrapper);
			DictCache.put(dict.toString()+page, result);
		}
		return TableResult.Result(result);

	}

	/**
	 * 根据类型查询下拉选,如果不存在该类型,自动添加一个
	 * @param dict
	 * @return
	 */
	@RequestMapping("/findDictsForSelect")
	@ResponseBody
	public TableResult findDictsForSelect(Dict dict) {

		TableResult dictList = findDictList(dict);
		List<Dict> result = (List<Dict>)dictList.getData();

		if(result==null||dictList.getData()==null||dictList.getCount()==0){
			dict.preInsert();
			dict.setLabel(dict.getType());
			boolean b = dictService.insert(dict);
			//  清空缓存
			DictCache.clear();
		}
		return dictList;

	}

	@RequestMapping("/deleteDict")
	@ResponseBody
	public TableResult deleteDict(Dict dict) {
		dict.preUpdate();
		boolean b = dictService.deleteById(dict.getId());
		//  清空缓存
		DictCache.clear();
		return TableResult.Result(b);
	}

	@RequestMapping("/deleteDictByIds")
	@ResponseBody
	public TableResult deleteDictByIds(String[] ids) {

		boolean b = dictService.deleteBatchIds(Arrays.asList(ids));
		//  清空缓存
		DictCache.clear();
		return TableResult.Result(b);
	}

	@RequestMapping("/getDictTypes")
	@ResponseBody
	public TableResult getDictTypes() {
		List<String> result = (List<String>) DictCache.get("getDictTypes");
		if (result == null) {
			result = dictService.getDictTypes();
			DictCache.put("getDictTypes", result);
		}
		return TableResult.Result(result);
	}

	@RequestMapping("/clearDictCache")
	@ResponseBody
	public TableResult clearDictCache() {
		//  清空缓存
		DictCache.clear();
		return TableResult.Result();
	}

}
